
package com.jf.cloud.platform.controller.platform;

import cn.hutool.core.util.BooleanUtil;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.platform.model.SysConfig;
import com.jf.cloud.platform.service.SysConfigService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * 网站配置信息表
 *
 * @author zz
 */
@RestController("platformWebConfigController")
@RequestMapping("/p/web_config")
@Tag(name = "网站配置信息表")
public class WebConfigController {

    @Autowired
    private SysConfigService sysConfigService;

    @Value("${jfcloud.expose.operation.auth:}")
    private Boolean permission;

    @GetMapping("/info/{key}")
    @Operation(summary = "根据key获取对应的网站配置" , description = "根据key获取对应的网站配置")
    public ServerResponseEntity<String> info(@PathVariable("key") String key) {
        return ServerResponseEntity.success(sysConfigService.getValue(key));
    }

    @PostMapping("/save")
    @Operation(summary = "更新网站配置" , description = "更新网站配置")
    public ServerResponseEntity<Void> save(@RequestBody @Valid SysConfig sysConfig) {
        if (BooleanUtil.isFalse(permission)) {
            return ServerResponseEntity.showFailMsg("对不起，您没有修改网站配置的权限");
        }
        if (Objects.isNull(sysConfig.getParamKey()) || Objects.isNull(sysConfig.getParamValue())) {
            throw new LuckException("参数不完整，请准确填写后重试");
        }
        sysConfigService.saveOrUpdateSysConfig(sysConfig);
        return ServerResponseEntity.success();
    }

}
